package other;

import java.util.Arrays;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        System.out.println(test01("dss dsd sds abc",3));
        System.out.println(test02("dss dsd sds abc",3));
    }

    public static String test01(String str,int n) {

        return Arrays.stream(str.split(" ")).limit(n).collect(Collectors.joining(" "));
    }

    public static String test02(String str,int n) {
        for(int i=0;i<str.length();i++) {
            if(str.charAt(i) == ' ') {
                n--;
            }
            if(n==0) {
                str = str.substring(0,i);
            }
        }
        return str;
    }
}
